/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.version.web;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.version.dao.AppParamDao;
import cn.apestar.modules.common.version.entity.AppParam;
import cn.apestar.modules.common.version.service.AppParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 更新Controller
 *
 * @author LVH
 * @version 2016-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/version/appParam")
public class AppParamController extends BaseController {

    @Autowired
    private AppParamService appParamService;

    @Autowired
    private AppParamDao appParamDao;

    @ModelAttribute
    public AppParam get(@RequestParam(required = false) String id) {
        AppParam entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = appParamService.get(id);
        }
        if (entity == null) {
            entity = new AppParam();
        }
        return entity;
    }

    @RequestMapping(value = { "list", "" })
    public String list(AppParam appParam, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<AppParam> page = appParamService.findPage(new Page<AppParam>(request, response), appParam);
        model.addAttribute("page", page);
        return "modules/version/appParamList";
    }

    @RequestMapping(value = "form")
    public String form(AppParam appParam, Model model) {
        model.addAttribute("appParam", appParam);
        return "modules/version/appParamForm";
    }

    @RequestMapping(value = "save")
    public String save(AppParam appParam, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, appParam)) {
            return form(appParam, model);
        }
        appParamService.save(appParam);
        addMessage(redirectAttributes, "保存更新成功");
        return "redirect:" + Global.getAdminPath() + "/version/appParam/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(AppParam appParam, RedirectAttributes redirectAttributes) {
        appParamService.delete(appParam);
        addMessage(redirectAttributes, "删除更新成功");
        return "redirect:" + Global.getAdminPath() + "/version/appParam/?repage";
    }

}